/*
 * Copyright (c) 2009 Project E7
 *
 * This file is part of ZebBowling
 *
 * ZebBowling is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ZebBowling is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ZebBowling. If not, see <http://www.gnu.org/licenses/>.
 **/

package org.zebbowling.control;

import java.util.ArrayList;

/**
 * This class is an auxilary class to enhance the fuctionality of ControllerConnections
 * by allowing controlled and coordinated use of mulitple connections.
 *
 * @author Joel Jauregui
 */
public class ControllerBaseStation {

    protected ArrayList connections;

    /**
     * Creates a new <code>ControllerBaseStation</code> which has no connections
     * associated with it
     */
    public ControllerBaseStation() {
        connections = new ArrayList();                
        }

    /**
     * Adds a connection to this base station that will be opened on call
     *
     * @param cc The connection to be added to this base station
     * @see #openConnAllPorts()
     */
    public void addConnection(ControllerConnection cc) {
        connections.add(cc);        
        }

     /**
      * This adds another port to this basestation that will be opened
      *
      * @param port The port of a new connection to be added
      * @see #openConnAllPorts()
      */
    public void addPort(int port) {
        ControllerConnection p = new ControllerConnection(port);
        addConnection(p);
        }

    /**
     * Adds an array of ports to this basestation
     *
     * @param ports The array of ports to be added
     * @see #addPort(int)
     * @see #openConnAllPorts()
     */
    public void addPorts(int[] ports) {
        for (int i = 0; i < ports.length; i++)
            addPort(ports[i]);
        }

    /**
     * Sets a ControlHandler to all ControllerConnections associated with this
     * ControllerBaseStation.
     *
     * @param ch The ControlHandler to add to all the ControllerConnections
     */
    public void setControlHandlers(ControlHandler[] ch) {
        for (int i = 0; i < connections.size(); i++)
            ((ControllerConnection)connections.get(i)).setControlHandler(ch[i]);
        }

    /**
     * Opens connections on all ports associated with this basestation
     *
     * @see #addPort(int)
     * @see #addPorts(int[])
     * @see #addConnection(org.zebbowling.control.ControllerConnection)
     */
    public void openConnAllPorts() {
        for (int i = 0; i < connections.size(); i++) {
            ControllerConnection cc = (ControllerConnection)connections.get(i);
            cc.start();
            }
        }   

    }
